package com.peng.svg;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.imageio.ImageIO;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

/**
 * SvgSample1
 *
 * @author lupeng
 * Created on 2022-07-12
 */
public class SvgSample1 {

    private static final Logger logger = LoggerFactory.getLogger(SvgSample1.class);

    //@Test
    //public void s1() {
    //    try {
    //        BufferedImage image = ImageIO.read(new File("/Users/hou/Desktop/privilege.png"));
    //
    //        SVGGraphics2D g2 = new SVGGraphics2D(image.getWidth(), image.getHeight());
    //        g2.drawImage(image, 0, 0, null);
    //
    //        g2.setPaint(Color.BLACK);
    //        g2.drawString("Hello SVG", 20, 50);
    //        String svgDocument = g2.getSVGDocument();
    //        System.out.println(svgDocument);
    //        List<ImageElement> svgImages = g2.getSVGImages();
    //        System.out.println(svgImages);
    //
    //        g2.getpng
    //
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //}

    @Test
    public void s2() throws Exception {
        DOMImplementation domImpl =
                GenericDOMImplementation.getDOMImplementation();

        // Create an instance of org.w3c.dom.Document.
        String svgNS = "http://www.w3.org/2000/svg";
        Document document = domImpl.createDocument(svgNS, "svg", null);
        BufferedImage image = ImageIO.read(new File("/Users/hou/Desktop/privilege.png"));
        // Create an instance of the SVG Generator.
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

        // Ask the test to render into the SVG Graphics2D implementation.
        svgGenerator.drawImage(image, 0, 0, null);
        svgGenerator.setPaint(Color.BLACK);
        svgGenerator.drawString("河北十强", 37, 80);
        //svgGenerator.fill(new Rectangle(10, 10, 100, 100));

        // Finally, stream out SVG to the standard output using
        // UTF-8 encoding.
        boolean useCSS = true; // we want to use CSS style attributes

        svgGenerator.stream(new BufferedWriter(new FileWriter("/Users/hou/learnProjects/simple-code/src/main/resources/svg/s1.svg")), useCSS);
    }

}
